package com.pzhu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pzhu.domain.Question;
import com.pzhu.domain.QuestionList;
import com.pzhu.domain.Result;
import com.pzhu.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/answer")
@CrossOrigin
@Slf4j
public class UserController {

    @Autowired
    private QuestionService questionService;


    @GetMapping
    public List<Question> allanswer() {
        return questionService.list();
    }

    /**
     * 根据查看详情.html解析答案
     *
     * @param httpServletRequest
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping("answer")
    public void parseanswer(HttpServletRequest httpServletRequest) throws ServletException, IOException {
        Collection<Part> parts = httpServletRequest.getParts();
        for (Part part : parts) {
            InputStream inputStream = part.getInputStream();
            Document document = Jsoup.parse(inputStream, "UTF-8", "");
            //获取每道题目
            Elements mark_name = document.getElementsByClass("marBom60");
            mark_name.forEach(e -> {
                Question question = new Question();
                String span = e.getElementsByClass("colorShallow").text();
                String h3 = e.getElementsByTag("h3").text();
                String[] split = h3.split(span);
                //获取题目
                question.setQuestion(split[1].substring(1));
                //获取答案
                String answer = e.getElementsByClass("colorGreen").text();
                question.setAnswer(answer);
                //获取选项
                Elements colorDeep = e.getElementsByClass("mark_letter colorDeep");
                if (colorDeep.size() != 0) {
                    Elements li = colorDeep.first().getElementsByTag("li");
                    question.setChoose1(li.get(0).text());
                    question.setChoose2(li.get(1).text());
                    if (li.size() > 2) {
                        question.setChoose3(li.get(2).text());
                        question.setChoose4(li.get(3).text());
                        switch (answer.split(": ")[1]) {
                            case "A":
                                question.setResult(question.getChoose1().split("\\. ")[1]);
                                break;
                            case "B":
                                question.setResult(question.getChoose2().split("\\. ")[1]);
                                break;
                            case "C":
                                question.setResult(question.getChoose3().split("\\. ")[1]);
                                break;
                            case "D":
                                question.setResult(question.getChoose4().split("\\. ")[1]);
                                break;
                        }
                    } else {
                        question.setResult(answer.split(":")[1]);
                    }
                } else {
                    question.setResult(answer);
                }
                //获取解析
                String analysis = e.getElementsByClass("analysis").text();
                question.setAnalysis(analysis);
                if (question.getAnswer() != "" && question.getAnswer() != null) questionService.save(question);
            });
        }
    }


    /**
     * 根据整卷预览.html解析答案
     *
     * @param httpServletRequest
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping
    public List<QuestionList> filepare(HttpServletRequest httpServletRequest) throws ServletException, IOException {
        ArrayList<QuestionList> questions = new ArrayList<>();
        InputStream file = httpServletRequest.getPart("file").getInputStream();
        Document document = Jsoup.parse(file, "UTF-8", "https://simplesolution.dev");
        Elements mark_name = document.getElementsByClass("mark_name");
        for (int i = 0; i < mark_name.size(); i++) {
            Element e = mark_name.get(i);
            String q = e.getElementsByTag("h3").text();
            String colorShallow = e.getElementsByClass("colorShallow").text();
            String[] split = q.split(colorShallow);
            LambdaQueryWrapper<Question> questionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            questionLambdaQueryWrapper.like(split[1] != null, Question::getQuestion, split[1].substring(2));
            List<Question> list = questionService.list(questionLambdaQueryWrapper);
            questions.add(new QuestionList(q, list));
        }
        return questions;
    }
    @PostMapping("/result")
    public List<Result> resultPare(HttpServletRequest httpServletRequest) throws ServletException, IOException {
        ArrayList<Result> answers = new ArrayList<>();
        InputStream file = httpServletRequest.getPart("file").getInputStream();
        Document document = Jsoup.parse(file, "UTF-8", "https://simplesolution.dev");
        Elements questionLi = document.getElementsByClass("questionLi");
        questionLi.forEach(e -> {
            String q = e.getElementsByTag("h3").text();
            String colorShallow = e.getElementsByClass("colorShallow").text();
            String[] split = q.split(colorShallow);
            Elements colorDeep = e.getElementsByClass("stem_answer");
            String choose1, choose2, choose3, choose4;
            LambdaQueryWrapper<Question> questionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            questionLambdaQueryWrapper.like(Question::getQuestion, split[1].substring(2));
            List<Question> list = questionService.list(questionLambdaQueryWrapper);
            if (list.size() != 0) {
                if ("stem_answer".equals(colorDeep.attr("class"))) {
                    Elements li = colorDeep.first().getElementsByClass("clearfix answerBg");
                    System.out.println(q);
                    choose1 = li.get(0).getElementsByClass("fl answer_p").text();
                    choose2 = li.get(1).getElementsByClass("fl answer_p").text();
                    if (li.size() > 2) {
                        choose3 = li.get(2).getElementsByClass("fl answer_p").text();
                        choose4 = li.get(3).getElementsByClass("fl answer_p").text();
                        Iterator<Question> iterator = list.iterator();
                        while (iterator.hasNext()) {
                            Question item = iterator.next();
                            String choose = item.getChoose1() + item.getChoose2() + item.getChoose3() + item.getChoose4();
                            if (!(choose.contains(choose1) &&
                                    choose.contains(choose2) &&
                                    choose.contains(choose3) &&
                                    choose.contains(choose4))) iterator.remove();
                        }
                        String result = list.get(0).getResult();
                        if (result.equals(choose1)) result = "A";
                        if (result.equals(choose2)) result = "B";
                        if (result.equals(choose3)) result = "C";
                        if (result.equals(choose4)) result = "D";
                        answers.add(new Result(split[0], result));
                    } else {
                        answers.add(new Result(split[0], list.get(0).getResult()));
                    }
                } else {
                    answers.add(new Result(split[0], list.get(0).getResult()));
                }
            } else {
                answers.add(new Result(split[0], "暂无答案"));
            }
        });
        return answers;
    }
}
